<template>
  <div class="dialog-form">
    <el-form ref="configRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="参数名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入参数名称" />
      </el-form-item>
      <el-form-item label="参数键名" prop="configKey">
          <el-input v-model="form.configKey" placeholder="请输入参数键名" />
      </el-form-item>
      <el-form-item label="参数键值" prop="configValue">
          <el-input v-model="form.configValue" placeholder="请输入参数键值" />
      </el-form-item>
      <el-form-item label="系统内置" prop="configType">
          <el-radio-group v-model="form.configType">
            <el-radio
                v-for="dict in sys_yes_no"
                :key="dict.value"
                :value="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getConfig, addConfig, updateConfig } from "@/api/system/config"

const configRef = ref()
const { proxy } = getCurrentInstance()
const { sys_yes_no } = proxy.useDict("sys_yes_no")

const emits = defineEmits(['cancel', 'confirm'])

const form = ref({
  configId: undefined,
  configName: undefined,
  configKey: undefined,
  configValue: undefined,
  configType: "Y",
  remark: undefined
})
const rules = {
  configName: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
  configKey: [{ required: true, message: "参数键名不能为空", trigger: "blur" }],
  configValue: [{ required: true, message: "参数键值不能为空", trigger: "blur" }]
}

const pageInit = (configId) => {
  if (configId) {
    getConfig(configId).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  configRef.value.validate(valid => {
    if (valid) {
      if (form.value.configId) {
        updateConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          configRef.value.resetFields()
          emits('confirm')
        });
      } else {
        addConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          configRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>